﻿using System;
using System.IO;
using Leadtools.Dicom;

namespace DICOMViewer.Utility
{
    public class DicomDirMaker
    {
        private DicomDirMaker()
        {
        }

        public static bool IsDicomPathCorrect(string workPath)
        {
            var filepathary = Directory.GetFiles(workPath, "*", SearchOption.AllDirectories);
            foreach (var s in filepathary)
            {
                if (isDicom(s)) return true;
            }
            return false;
        }

        public static void CreateDicomDirectory(string workPath)
        {
            try
            {
                DicomDir dicomDir = new DicomDir();
                // Reset the DICOM Directory and set the destination folder where
                // the DICOMDIR file wiUserDicomDirll be saved
                dicomDir.Reset(workPath);

                // If it is desired to change the values of the Implementation Class
                // UID (0002,0012) and the Implementation Version Name (0002,0013)...
                DicomElement element;
                element = dicomDir.DataSet.FindFirstElement(null, DicomTag.ImplementationClassUID, false);
                if (element != null)
                {
                    dicomDir.DataSet.SetStringValue(element, "1.2.840.114257.0.1", DicomCharacterSetType.Default);
                        // Must be a UID
                }
                element = dicomDir.DataSet.FindFirstElement(null, DicomTag.ImplementationVersionName, false);
                if (element != null)
                {
                    dicomDir.DataSet.SetStringValue(element, "LEADTOOLS 15", DicomCharacterSetType.Default);
                        // Must be a UID
                }

                // Set options
                DicomDirOptions options = dicomDir.Options;
                options.IncludeSubfolders = true;
                options.InsertIconImageSequence = false;
                options.RejectInvalidFileId = false;
                dicomDir.Options = options;

                // Add the DICOM files to the DICOM Directory.
                // This is the function that does it all!
                // You can always give the user feedback about the progress inside 
                // this function by overriding the function DicomDir.OnInsertFile.   
                dicomDir.InsertFile(null);
                dicomDir.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool isDicom(string filePath)
        {
            FileStream fs = File.OpenRead(filePath);
            if (fs.Length < 132) return false;
            var data = new byte[132];
            fs.Read(data, 0, data.Length);
            fs.Position = 0;
            int b0 = data[0] & 255, b1 = data[1] & 255, b2 = data[2] & 255, b3 = data[3] & 255;

            if (data[128] == 68 && data[129] == 73 && data[130] == 67 && data[131] == 77)
            {
                return true;
            }
            if ((b0 == 8 || b0 == 2) && b1 == 0 && b3 == 0)
            {
                return true;
            }
            return false;
        }

        public static bool isDicom(Stream fs)
        {
            if (fs.Length < 132) return false;
            var data = new byte[132];
            fs.Read(data, 0, data.Length);
            fs.Position = 0;
            int b0 = data[0] & 255, b1 = data[1] & 255, b2 = data[2] & 255, b3 = data[3] & 255;

            if (data[128] == 68 && data[129] == 73 && data[130] == 67 && data[131] == 77)
            {
                return true;
            }
            if ((b0 == 8 || b0 == 2) && b1 == 0 && b3 == 0)
            {
                return true;
            }
            return false;
        }
    }
}